<template>
  <div class="pagination">
    <!-- <button @click="toPrev" :disabled="pageNo === 1 ? true : false">
      上一页
    </button> -->
    <button
      @click="toPrev"
      :style="{ cursor: activeCursor, color: activeColor }"
    >
      上一页
    </button>
    <button
      v-show="startAndEnd.start > 1"
      @click="toPageNo(1)"
      :class="{ active: pageNo === 1 }"
    >
      1
    </button>
    <span v-show="startAndEnd.start > 2">...</span>
    <button
      @click="toPageNo(startAndEnd.start + index)"
      :class="{ active: startAndEnd.start + index === pageNo }"
      v-for="(item, index) in continues"
      :key="item"
    >
      {{ startAndEnd.start + index }}
    </button>
    <span v-show="startAndEnd.end < totalPage - 1">···</span>
    <button
      @click="toPageNo(totalPage)"
      v-show="startAndEnd.end < totalPage"
      :class="{ active: pageNo === totalPage }"
    >
      {{ totalPage }}
    </button>
    <button @click="toNext" :disabled="pageNo === totalPage">下一页</button>

    <span>共 {{ total }} 条</span>&nbsp;&nbsp;
    <span>到第</span>
    <input v-model="page" type="text" />
    <span>页</span>
    <button @click="sendPage">确定</button>
  </div>
</template>

<script>
import { regPage } from '@/utils/reg.js'
export default {
  name: 'Pagination',
  data() {
    return {
      page: 1,
    }
  },
  props: {
    total: { type: Number, default: 5 },
    pageNo: { type: Number, default: 1 },
    pageSize: { type: Number, default: 5 },
    continues: { type: Number },
  },
  computed: {
    //1.计算总页码数
    totalPage() {
      let { total, pageSize } = this
      return Math.ceil(total / pageSize)
    },
    //2.计算连贯页数的起始位置
    startAndEnd() {
      let { pageNo, continues, totalPage } = this
      let start, end
      if (totalPage < continues) {
        start = 1
        end = totalPage
      } else {
        start = pageNo - parseInt(continues / 2)
        end = pageNo + parseInt(continues / 2)
        if (start < 1) {
          start = 1
          end = continues
        } else if (end > totalPage) {
          end = totalPage
          start = end - continues + 1
        }
      }
      return { start, end }
    },
    activeCursor() {
      if (this.pageNo === 1) {
        return 'default'
      } else {
        return 'pointer'
      }
    },
    activeColor() {
      if (this.pageNo === 1) {
        return '#ccc'
      } else {
        return '#606266'
      }
    },
  },
  methods: {
    //1.点击去指定页面
    toPageNo(newPage) {
      this.page = newPage
      this.$emit('get-pageNo', newPage)
    },
    //2.点击去上一页
    toPrev() {
      if (this.pageNo !== this.startAndEnd.start) {
        this.$emit('get-pageNo', this.pageNo - 1)
      }
    },
    //3.点击去下一页
    toNext() {
      if (this.pageNo !== this.totalPage) {
        this.$emit('get-pageNo', this.pageNo + 1)
      }
    },
    //4.页面跳转按钮
    sendPage() {
      let { page } = this
      this.$emit('get-pageNo', page)
    },
  },
  watch: {
    page(newValue) {
      if (regPage.test(this.page)) {
        this.page = Number(newValue)
        if (this.page > this.totalPage) {
          this.page = this.totalPage
        }
      } else {
        this.page = 1
      }
    },
  },
}
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #c81623;
      color: #fff;
    }
  }
  input {
    width: 25px;
    height: 25px;
    padding-left: 10px;
  }
  span {
    display: inline-block;
    line-height: 28px;
    font-size: 14px;
    color: gray;
    vertical-align: middle;
  }
}
</style>
